#!/bin/bash
start_line=172.16.105.100
dns_line=172.16.105.99
end_line=172.16.105.200
subnet=172.16.105.0
ISOUrl='http://172.16.105.2/Image/ISO/CentOS-7-x86_64-DVD-1810.iso'
passwd='cloudadmin'


yum install -y net-tools
ISO=`echo $ISOUrl | awk -F '/' '{print $NF}'` 
ISOName=`echo $ISO |awk -F '.' '{print $1}'`
local_ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
#subnet=`ifconfig | grep inet | head -n 1 | awk '{print $2}' | awk -F . '{print $1"."$2"."$3"."}'`


sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld && systemctl disable firewalld.service
yum install epel-release -y 
yum install -y cobbler cobbler-web tftp* rsync xinetd http* syslinux dhcp* pykickstart wget vim
systemctl enable xinetd && systemctl restart xinetd
systemctl enable httpd.service && systemctl restart httpd.service
systemctl enable cobblerd.service && systemctl restart cobblerd.service
sed -i 's/server: 127.0.0.1/server: '${local_ip}'/' /etc/cobbler/settings
sed -i 's/next_server: 127.0.0.1/next_server: '${local_ip}'/' /etc/cobbler/settings
sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings
sed -ri "/default_password_crypted/s#(.*: ).*#\1\"`openssl passwd -1 -salt 'root' ${passwd}`\"#" /etc/cobbler/settings
sed -i 's#yes#no#' /etc/xinetd.d/tftp
sed -i '0,/subnet\ .*/s//subnet '${subnet}' netmask 255.255.255.0\ {/' /etc/cobbler/dhcp.template
sed -i '0,/option\ routers\ .*/s//option\ routers\ '${subnet}'1;/' /etc/cobbler/dhcp.template
sed -i '0,/option\ domain-name-servers\ .*/s//option\ domain-name-servers\ '${dns_line}';/' /etc/cobbler/dhcp.template
sed -i '0,/option\ subnet-mask\ .*/s//option\ subnet-mask\ 255.255.255.0;/' /etc/cobbler/dhcp.template
sed -i '0,/range\ dynamic-bootp\ .*/s//range\ dynamic-bootp\ '${start_line}'\ '${end_line}';/' /etc/cobbler/dhcp.template
systemctl enable rsyncd && systemctl restart rsyncd
cobbler get-loaders
systemctl restart tftp.socket
systemctl restart xinetd.service
systemctl restart httpd.service
systemctl restart cobblerd.service
systemctl restart rsyncd
cobbler sync
wget $ISOUrl
mount $ISO /mnt
cobbler import --path=/mnt/ --name=$ISOName --arch=x86_64 


